<template>
<!-- 主体结构 -->
  <div>
    <!-- 卡片 -->
    <el-card>
      <!-- 标表单 -->
      <el-form  label-width="80px">
        <el-row>
          <el-col :span="6" >
            <el-form-item label="标签名称">
            <el-input placeholder="请输入" v-model="searchOption.tags"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" >
            <el-form-item label="城市">
              <!-- 城市下拉列表 -->
            <el-select v-model="searchOption.province" size="medium" placeholder="请选择">
              <el-option
                v-for="item in cityOption"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" >
            <el-form-item label="地区">
              <!-- 城市下拉列表 -->
            <el-select v-model="searchOption.city" placeholder="请选择">
              <el-option
                v-for="item in areaOption"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" >
            <el-form-item label="企业简称">
            <el-input v-model="searchOption.shortName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item label="状态">
             <el-select v-model="searchOption.state" placeholder="请选择">
              <!-- 状态下拉列表 -->
              <el-option
                v-for="item in stateOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="1">
            <!-- 按钮 -->
            <el-button @click="clearOption">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-col>
          <el-col :span="11" class="titleBtn">
            <!-- 新增 -->
             <el-button type="success" icon="el-icon-edit" @click="handerEdit('add')">新增用户</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 记录 -->
      <el-alert
        type="info"
        :title="`共${searchRes.counts}条记录`"
        show-icon>
      </el-alert>
      <!-- 用户列表 -->
      <el-table
      :data="searchRes.items"
      style="width: 100%"
      header-align="center"
      >
        <!-- 序号 -->
        <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="180">
        </el-table-column>
        <!-- 编号 -->
        <el-table-column
        prop="number"
        label="企业编号"
        align="center"
        width="180">
        </el-table-column>
        <!-- 简称 -->
        <el-table-column
        prop="shortName"
        align="center"
        label="企业简称">
        </el-table-column>
        <!-- 标签 -->
        <el-table-column
        prop="tags"
        label="标签"
        align="center">
        </el-table-column>
        <!-- 创建者 -->
        <el-table-column
        prop="creatorID"
        label="创建者"
        align="center">
        </el-table-column>
        <!-- 创建日期 -->
        <el-table-column
        prop="addDate"
        label="创建日期"
        align="center">
        </el-table-column>
        <!-- 备注 -->
        <el-table-column
        prop="remarks"
        label="备注"
        align="center">
        </el-table-column>
        <!-- 状态 -->
        <el-table-column
        prop="state"
        label="状态"
        align="center">
        <template slot-scope="{row}">
          <span>{{row.state===0?'禁用':'启用'}}</span>
        </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
        prop="tags"
        label="操作"
         width="200"
         align="center">
         <!-- 作用域插槽 -->
        <template slot-scope="{row}">
          <!-- 编辑 -->
          <el-button
          circle
          plain
          type="primary"
          icon="el-icon-edit"
          @click="handerEdit('edit', row)">
          </el-button>
          <!-- 禁用 -->
          <el-button
          circle
          plain
          type="success"
          icon="el-icon-check"
          v-if="row.state===0"
          @click="changeState(row)">
          </el-button>
          <!-- 启用 -->
          <el-button
          circle
          plain
          type="warning"
          icon="el-icon-close"
          v-else
          @click="changeState(row)">
          </el-button>
          <!-- 删除 -->
          <el-button
          circle
          plain
          type="danger"
          icon="el-icon-delete"
          @click="handleDelete(row)">
          </el-button>
      </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="bottomTool">
      <PageTool :total='searchRes.counts' :paginationPage='searchOption.page' :paginationPagesize='searchOption.pagesize' @pageChange='pageChange' @pageSizeChange='pageSizeChange'>
      </PageTool>
      </div>
    </el-card>
    <!-- 弹框 -->
    <CompanysAdd ref="companyAdd" :formBase='formBase' :titleInfo='titleInfo' @newDataes='newDataes'></CompanysAdd>
  </div>
</template>

<script>
import CompanysAdd from '@/module-hmmm/components/companys-add'/* 新增或编辑弹框 */
import { provinces, citys } from '@/api/hmmm/citys.js'/* 获取城市，地区 */
import { list, disabled, remove } from '@/api/hmmm/companys.js'/* 请求数据 */
import PageTool from '@/module-manage/components/page-tool'/* 分页器 */
export default {
  data () {
    return {
      // 表单数据源
      searchOption: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: '',
        pagesize: 10,
        page: 1
      },
      cityOption: [], /* 城市列表 */
      areaOption: [], /* 地区列表 */
      stateOption: [{ label: '启用', value: 1 }, { label: '禁用', value: 0 }], /* 状态列表 */
      searchRes: [], /* 用户列表信息 */
      titleInfo: { text: '', pageTitle: '' }, /* 弹框标题名 */
      // 弹框数据源
      formBase: {
        shortName: '',
        isFamous: false,
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      }
    }
  },
  components: { PageTool, CompanysAdd },
  // 一开始获取城市信息，用户列表
  async created () {
    this.cityOption = provinces()
    this.getSearchOption()
  },
  watch: {
    'searchOption.province': {
      handler (value) {
        this.areaOption = citys(value)
        this.searchOption.city = ''
      }
    }
  },
  methods: {
    // 获取用户信息
    async getSearchOption () {
      const { data } = await list(this.getValue(this.searchOption))
      this.searchRes = data
    },
    // 清除
    clearOption () {
      this.searchOption = {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: '',
        pagesize: 10,
        page: 1
      }
    },
    // 搜索
    search () {
      this.getSearchOption()/* 用户列表 */
    },
    getValue (data) {
      const obj = {}
      for (const k in data) {
        if (data[k] || data[k] === 0) {
          obj[k] = data[k]
        }
      }
      return obj
    },
    // 进入某一页
    pageChange (v) {
      this.searchOption.page = v
      this.getSearchOption()/* 用户列表 */
    },
    // 每页条数
    pageSizeChange (v) {
      this.searchOption.pagesize = v
      this.getSearchOption()/* 用户列表 */
    },
    // 新增或编辑
    handerEdit (type, value) {
      //
      this.formBase = {
        shortName: '',
        isFamous: false,
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      }
      if (type === 'add') {
        // 增加用户
        this.titleInfo.text = '创建用户'
        this.$refs.companyAdd.dialogFormV()/* 弹层显示 */
      } else if (type === 'edit') {
        // 编辑用户
        this.titleInfo.text = '编辑用户'
        this.formBase = value
        this.formBase.isFamous = false
        this.$refs.companyAdd.dialogFormV()/* 弹层显示 */
      }
    },
    // 重新渲染
    newDataes () {
      this.getSearchOption()/* 用户列表 */
    },
    // 切换状态
    changeState (row) {
      // 警示框
      this.$confirm('此操作将改变状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await disabled({ id: row.id, state: row.state === 0 ? 1 : 0 })
        // 提示
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.getSearchOption()/* 用户列表 */
      }).catch(() => {
        // 提示
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 删除用户
    handleDelete (row) {
      // 警示框
      this.$confirm('此操作将删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.id })
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.getSearchOption()/* 用户列表 */
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }

  }
}
</script>

<style scoped lang='less'>
.el-select{
  display: block;
}
.bottomTool{
    margin: 20px 0;
    float: right;
  }
.titleBtn{
  position: relative;
  .el-button{
    position: absolute;
    right: 0;
  }
}

</style>
